| Conditions | 2 | 
| Paths | 2 | 
| Total Lines | 118 | 
| Code Lines | 65 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | 'use strict'  | 
            ||
| 11 | module.exports = (baseFileDir, existingIssue, source, params, callback) => { | 
            ||
| 12 | |||
| 13 |     if ( existingIssue ) { | 
            ||
| 14 |         return updateIssue((error) => { | 
            ||
| 15 | callback(error, existingIssue)  | 
            ||
| 16 | })  | 
            ||
| 17 | }  | 
            ||
| 18 | |||
| 19 |     return createIssue((error, newIssue) => { | 
            ||
| 20 | callback(error, newIssue)  | 
            ||
| 21 | })  | 
            ||
| 22 | |||
| 23 |     function createIssue (done) { | 
            ||
| 24 |         debug('Issue doesn\'t exist, creating new issue...') | 
            ||
| 25 | |||
| 26 |         return requestIssue(source.url + '/rest/api/2/issue/', 'POST', (error, response, body) => { | 
            ||
| 27 |             if ( !error && !body ) { | 
            ||
| 28 |                 return done(new Error('Could not create issue.')) | 
            ||
| 29 | }  | 
            ||
| 30 | |||
| 31 | done(error, body)  | 
            ||
| 32 | })  | 
            ||
| 33 | }  | 
            ||
| 34 | |||
| 35 |     function updateIssue (done) { | 
            ||
| 36 | let issueId = existingIssue.id  | 
            ||
| 37 | let issueKey = existingIssue.key  | 
            ||
| 38 | |||
| 39 |         debug('Issue exists [%s], updating issue...', issueKey) | 
            ||
| 40 | |||
| 41 | return requestIssue(source.url + '/rest/api/2/issue/' + issueId, 'PUT', done)  | 
            ||
| 42 | }  | 
            ||
| 43 | |||
| 44 |     function requestIssue (issueUrl, method, callback) { | 
            ||
| 45 | |||
| 46 |         let issue = { | 
            ||
| 47 | fields: processFields()  | 
            ||
| 48 | }  | 
            ||
| 49 | |||
| 50 |         debug('Sending issue: %s', JSON.stringify(issue, null, 2)) | 
            ||
| 51 | |||
| 52 |         request({ | 
            ||
| 53 | method: method,  | 
            ||
| 54 | uri: issueUrl,  | 
            ||
| 55 |             auth:   { | 
            ||
| 56 | username: source.username,  | 
            ||
| 57 | password: source.password  | 
            ||
| 58 | },  | 
            ||
| 59 | json: issue  | 
            ||
| 60 |         }, (error, response, body) => { | 
            ||
| 61 |             if ( error ) { | 
            ||
| 62 | return callback(error)  | 
            ||
| 63 | }  | 
            ||
| 64 | |||
| 65 | debugResponse(response)  | 
            ||
| 66 | |||
| 67 |             if ( response.statusCode < 200 || 300 <= response.statusCode ) { | 
            ||
| 68 |                 return callback(new Error('Could not update Jira.')) | 
            ||
| 69 | }  | 
            ||
| 70 | |||
| 71 | callback(error, response, body)  | 
            ||
| 72 | })  | 
            ||
| 73 | }  | 
            ||
| 74 | |||
| 75 |     function processFields () { | 
            ||
| 76 |         let fields = params.fields || {} | 
            ||
| 77 | |||
| 78 | fields.summary = params.summary  | 
            ||
| 79 | |||
| 80 | fields = _.merge(parseCustomFields(params), fields)  | 
            ||
| 81 | |||
| 82 | fields = _(fields)  | 
            ||
| 83 |             .mapValues((value) => { | 
            ||
| 84 | return replaceTextFileString(baseFileDir, value)  | 
            ||
| 85 | })  | 
            ||
| 86 | .mapValues(replaceNowString)  | 
            ||
| 87 | .value()  | 
            ||
| 88 | |||
| 89 |         fields.project = { key: source.project } | 
            ||
| 90 | |||
| 91 |         if ( params.issue_type ) { | 
            ||
| 92 |             fields.issuetype = { name: params.issue_type } | 
            ||
| 93 | }  | 
            ||
| 94 | |||
| 95 | return fields  | 
            ||
| 96 | }  | 
            ||
| 97 | |||
| 98 |     function replaceNowString (value) { | 
            ||
| 99 | value = String(value)  | 
            ||
| 100 | |||
| 101 |         return value.replace(/\$NOW([-+][0-9]+)?([ywdhms])?/, (match, change, unit) => { | 
            ||
| 102 | let date = moment()  | 
            ||
| 103 | |||
| 104 | unit = unit || 'm'  | 
            ||
| 105 | |||
| 106 |             if ( change ) { | 
            ||
| 107 | date = date.add(change, unit)  | 
            ||
| 108 | }  | 
            ||
| 109 | |||
| 110 | return date.format()  | 
            ||
| 111 | })  | 
            ||
| 112 | }  | 
            ||
| 113 | |||
| 114 |     function parseCustomFields (params) { | 
            ||
| 115 |         if ( !params.custom_fields ) { | 
            ||
| 116 |             return {} | 
            ||
| 117 | }  | 
            ||
| 118 | |||
| 119 | return _(params.custom_fields)  | 
            ||
| 120 |             .mapKeys((value) => { | 
            ||
| 121 | return 'customfield_' + value.id  | 
            ||
| 122 | })  | 
            ||
| 123 |             .mapValues((value) => { | 
            ||
| 124 | return value.value  | 
            ||
| 125 | })  | 
            ||
| 126 | .value()  | 
            ||
| 127 | }  | 
            ||
| 128 | }  | 
            ||
| 129 |